package com.itcast.controller;

import com.itcast.pojo.Borrow;
import com.itcast.pojo.Result;
import com.itcast.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/borrow")
public class BorrowController {
    @Autowired
    private BorrowService borrowService;

    /**
     * 借阅图书
     * @param bookId 借阅图书id
     * @param request
     * @return
     */
    @PostMapping("/{bookId}")
    public Result borrow(@PathVariable Integer bookId, HttpServletRequest request){
        borrowService.borrow(bookId,request);
        return Result.success();
    }

    /**
     * 展示用户借阅记录
     * @param request
     * @return
     */
    @GetMapping
    public Result display(HttpServletRequest request){
        List<Borrow> list = borrowService.display(request);
        return Result.success(list);
    }

    /**
     * 归还图书
     * @param id 借阅记录的id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result returnBook(@PathVariable Integer id){
        borrowService.returnBook(id);
        return Result.success();
    }
}
